/**
 * Created by George on 2018/2/23.
 */
define(['angular', 'util'], function (angular, util) {
    "use strict";
    // 将本控制器函数作为结果返回给router.js
    var editUserCtrl = function ($scope, $http, $stateParams) {
        $scope.welcome_info = "会员管理";
        $scope.user = {
            id: "",
            name: "",
            info: "",
            joinDate: util.getNowFormatDate()
        };

        $scope.editUser = function () {
            var data = {
                "id": $scope.user.id,
                "name": $scope.user.name,
                "info": $scope.user.info,
                "joinDate": $scope.user.joinDate
            };
            $http.put('/basketballfire/rest/user', data).then(function (req) {
                console.log("success!");
                alert("修改成功！");
                window.location.href = "/basketballfire/index.html#/user";
            }, function (req) {
                console.log("failed!");
                alert("修改失败！");
            });
        };

        $scope.cancel = function () {
            window.location.href = "/basketballfire/index.html#/user";
        };

        function initOrUpdateData() {
            $scope.user = {
                id: "",
                name: "",
                info: "",
                joinDate: util.getNowFormatDate()
            };

            var userID = $stateParams.userId;

            $http.get("/basketballfire/rest/user?id=" + userID).then(function (data, status) {
                console.log(data);
                var usersData = data.data;
                $scope.user = {
                    id: usersData.id,
                    name: usersData.name,
                    info: usersData.info,
                    joinDate: usersData.joinDate
                };
            });
        }

        function init() {
            initOrUpdateData();
        }

        //ctrl start
        init();
    };

    return editUserCtrl;
});